<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item :label="t('templateRule.fields.templateId')" prop="templateId">
        <el-input
          v-model="formData.templateId"
          :placeholder="t('common.inputTextPlaceholder') + t('templateRule.fields.templateId')"
        />
      </el-form-item>
      <el-form-item :label="t('templateRule.fields.judgeType')" prop="judgeType">
        <el-select
          v-model="formData.judgeType"
          :placeholder="t('common.selectTextPlaceholder') + t('templateRule.fields.judgeType')"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('templateRule.fields.judgeStage')" prop="judgeStage">
        <el-input
          v-model="formData.judgeStage"
          :placeholder="t('common.inputTextPlaceholder') + t('templateRule.fields.judgeStage')"
        />
      </el-form-item>
      <el-form-item :label="t('templateRule.fields.isDrawToNext')" prop="isDrawToNext">
        <el-radio-group v-model="formData.isDrawToNext">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="t('templateRule.fields.isDebate')" prop="isDebate">
        <el-radio-group v-model="formData.isDebate">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="t('templateRule.fields.isMajorGroup')" prop="isMajorGroup">
        <el-radio-group v-model="formData.isMajorGroup">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="t('templateRule.fields.majorGroupSettings')" prop="majorGroupSettings">
        <el-input
          v-model="formData.majorGroupSettings"
          :placeholder="
            t('common.inputTextPlaceholder') + t('templateRule.fields.majorGroupSettings')
          "
        />
      </el-form-item>
      <el-form-item :label="t('templateRule.fields.scoreJson')" prop="scoreJson">
        <el-input
          v-model="formData.scoreJson"
          :placeholder="t('common.inputTextPlaceholder') + t('templateRule.fields.scoreJson')"
        />
      </el-form-item>
      <el-form-item :label="t('templateRule.fields.judgeRule')" prop="judgeRule">
        <el-input
          v-model="formData.judgeRule"
          :placeholder="t('common.inputTextPlaceholder') + t('templateRule.fields.judgeRule')"
        />
      </el-form-item>
      <el-form-item :label="t('templateRule.fields.totalAmount')" prop="totalAmount">
        <el-input
          v-model="formData.totalAmount"
          :placeholder="t('common.inputTextPlaceholder') + t('templateRule.fields.totalAmount')"
        />
      </el-form-item>
      <el-form-item :label="t('templateRule.fields.phdAmount')" prop="phdAmount">
        <el-input
          v-model="formData.phdAmount"
          :placeholder="t('common.inputTextPlaceholder') + t('templateRule.fields.phdAmount')"
        />
      </el-form-item>
      <el-form-item :label="t('templateRule.fields.masterAmount')" prop="masterAmount">
        <el-input
          v-model="formData.masterAmount"
          :placeholder="t('common.inputTextPlaceholder') + t('templateRule.fields.masterAmount')"
        />
      </el-form-item>
      <el-form-item :label="t('templateRule.fields.remark')" prop="remark">
        <el-input
          v-model="formData.remark"
          :placeholder="t('common.inputTextPlaceholder') + t('templateRule.fields.remark')"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading"
        >{{ t('common.ok') }}
      </el-button>
      <el-button @click="dialogVisible = false">{{ t('common.cancel') }}</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { TemplateRuleApi, TemplateRuleVO } from '@/api/awards/templaterule'
import { FormRules } from 'element-plus'

/** 奖项阶段规则设置 表单 */
defineOptions({ name: 'TemplateRuleForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  templateId: undefined,
  judgeType: undefined,
  judgeStage: undefined,
  isDrawToNext: undefined,
  isDebate: undefined,
  isMajorGroup: undefined,
  majorGroupSettings: undefined,
  scoreJson: undefined,
  judgeRule: undefined,
  totalAmount: undefined,
  phdAmount: undefined,
  masterAmount: undefined,
  remark: undefined
})
const formRules = reactive<FormRules>({
  id: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('templateRule.fields.id'),
      trigger: 'blur'
    }
  ],
  templateId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('templateRule.fields.templateId'),
      trigger: 'blur'
    }
  ],
  judgeType: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('templateRule.fields.judgeType'),
      trigger: 'change'
    }
  ],
  judgeStage: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('templateRule.fields.judgeStage'),
      trigger: 'blur'
    }
  ],
  isDrawToNext: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('templateRule.fields.isDrawToNext'),
      trigger: 'blur'
    }
  ],
  isDebate: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('templateRule.fields.isDebate'),
      trigger: 'blur'
    }
  ],
  isMajorGroup: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('templateRule.fields.isMajorGroup'),
      trigger: 'blur'
    }
  ],
  majorGroupSettings: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('templateRule.fields.majorGroupSettings'),
      trigger: 'blur'
    }
  ],
  scoreJson: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('templateRule.fields.scoreJson'),
      trigger: 'blur'
    }
  ],
  judgeRule: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('templateRule.fields.judgeRule'),
      trigger: 'blur'
    }
  ],
  totalAmount: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('templateRule.fields.totalAmount'),
      trigger: 'blur'
    }
  ],
  phdAmount: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('templateRule.fields.phdAmount'),
      trigger: 'blur'
    }
  ],
  masterAmount: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('templateRule.fields.masterAmount'),
      trigger: 'blur'
    }
  ],
  remark: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('templateRule.fields.remark'),
      trigger: 'blur'
    }
  ],
  creator: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('templateRule.fields.creator'),
      trigger: 'blur'
    }
  ],
  createTime: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('templateRule.fields.createTime'),
      trigger: 'blur'
    }
  ],
  updater: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('templateRule.fields.updater'),
      trigger: 'blur'
    }
  ],
  updateTime: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('templateRule.fields.updateTime'),
      trigger: 'blur'
    }
  ],
  deleted: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('templateRule.fields.deleted'),
      trigger: 'blur'
    }
  ],
  tenantId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('templateRule.fields.tenantId'),
      trigger: 'blur'
    }
  ]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: string) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await TemplateRuleApi.getTemplateRule(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as TemplateRuleVO
    if (formType.value === 'create') {
      await TemplateRuleApi.createTemplateRule(data)
      message.success(t('common.createSuccess'))
    } else {
      await TemplateRuleApi.updateTemplateRule(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    templateId: undefined,
    judgeType: undefined,
    judgeStage: undefined,
    isDrawToNext: undefined,
    isDebate: undefined,
    isMajorGroup: undefined,
    majorGroupSettings: undefined,
    scoreJson: undefined,
    judgeRule: undefined,
    totalAmount: undefined,
    phdAmount: undefined,
    masterAmount: undefined,
    remark: undefined
  }
  formRef.value?.resetFields()
}
</script>
